/*
 * 文件名： LocationStateTag.java
 * 
 * 创建日期： 2016年11月29日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */
package com.moli.iov.protocol.enums.realtime;

import com.moli.iov.protocol.enums.IDictsEnumTag;
import com.moli.iov.util.ByteArrayUtil;

/**
 * 车辆定位状态
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年11月29日
 */
public class LocationStateTag implements IDictsEnumTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 存放单字节8位
	private byte[] locationBytes =
		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	public LocationStateTag(IsValid isValid, LAT lat,LON lon) {
		setIsValid(isValid);
		setLat(lat);
		setLon(lon);
	}
	public LocationStateTag() {
	}
	private String k;

	private byte v;
	
	/**
	 * 设置定位数据
	 * 
	 * @param isValid
	 * @param lat
	 * @param lon
	 */
	public void setLocation(IsValid isValid, LAT lat,LON lon) {
		setIsValid(isValid);
		setLat(lat);
		setLon(lon);
	}

	/**
	 * 设置 是否定位有效
	 * 
	 * @param yes_no
	 */
	public void setIsValid(IsValid isValid) {
		locationBytes[7 - 0] = isValid.getValue();
	}
	/**
	 * 获取 是否有效定位
	 * 
	 * @return IsValid
	 */
	public IsValid getIsValid(){
		return IsValid.valOf(locationBytes[7 - 0]);
	}
	/**
	 * 设置纬度
	 * 
	 * @param Lat
	 */
	public void setLat(LAT lat) {
		locationBytes[7 - 1] = lat.getValue();
	}
	/**
	 * 获取纬度类型
	 * 
	 * @return Lat
	 */
	public LAT getLat(){
		return LAT.valOf(locationBytes[7 - 1]);
	}
	/**
	 * 设置经度
	 * 
	 * @param Lon
	 */
	public void setLon(LON lon) {
		locationBytes[7 - 2] = lon.getValue();
	}
	/**
	 * 获取经度类型
	 * 
	 * @return LON
	 */
	public LON getLon(){
		return LON.valOf(locationBytes[7 - 2]);
	}
	/**
	 *
	 */
	@Override
	public byte getV() {
		StringBuilder bitsBuilder = new StringBuilder();
		for (byte b : locationBytes) {
			bitsBuilder.append(b);
		}
		this.v = ByteArrayUtil.bitToByte(bitsBuilder.toString());
		return this.v;
	}

	/**
	 *
	 */
	@Override
	public String getK() {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(IsValid.getKey(locationBytes[7]) + "-" + LAT.getKey(locationBytes[6]) + "-"
				+ LON.getKey(locationBytes[5]));
		this.k = sBuilder.toString();
		return this.k;
	}
	/**
	 * 是否有效定位 枚举
	 *
	 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
	 *
	 * @version $Revision$
	 *
	 * @since 2016年12月1日
	 */
	public enum IsValid {
		YES("定位", (byte) 0x00), NO("未定位", (byte) 0x01);
		private String key;

		private byte value;

		IsValid(String key, byte value) {
			this.key = key;
			this.value = value;
		}

		public static IsValid valOf(byte value) {
			switch (value) {
			case 0x00:
				return YES;
			case 0x01:
				return NO;
			default:
				return YES;
			}
		}

		public static String getKey(byte value) {
			for (IsValid c : IsValid.values()) {
				if (c.getValue() == value) {
					return c.key;
				}
			}
			return null;
		}

		public String getKey() {
			return this.key;
		}

		public byte getValue() {
			return this.value;
		}
	}
	/**
	 * 纬度 枚举
	 *
	 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
	 *
	 * @version $Revision$
	 *
	 * @since 2016年12月1日
	 */
	public enum LAT {
		NORTH("北纬", (byte) 0x00), SOUTH("南纬", (byte) 0x01);
		private String key;

		private byte value;

		LAT(String key, byte value) {
			this.key = key;
			this.value = value;
		}

		public static LAT valOf(byte value) {
			switch (value) {
			case 0x00:
				return NORTH;
			case 0x01:
				return SOUTH;
			default:
				return NORTH;
			}
		}

		public static String getKey(byte value) {
			for (LAT c : LAT.values()) {
				if (c.getValue() == value) {
					return c.key;
				}
			}
			return null;
		}

		public String getKey() {
			return this.key;
		}

		public byte getValue() {
			return this.value;
		}
	}
	/**
	 * 经度 枚举
	 *
	 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
	 *
	 * @version $Revision$
	 *
	 * @since 2016年12月1日
	 */
	public enum LON {

		EAST("东经", (byte) 0x00), WEST("西经", (byte) 0x01);

		private String key;

		private byte value;

		LON(String key, byte value) {
			this.key = key;
			this.value = value;
		}

		public static LON valOf(byte value) {
			switch (value) {
			case 0x00:
				return EAST;
			case 0x01:
				return WEST;
			default:
				return EAST;
			}
		}

		public static String getKey(byte value) {
			for (LON c : LON.values()) {
				if (c.getValue() == value) {
					return c.key;
				}
			}
			return null;
		}

		public String getKey() {
			return this.key;
		}

		public byte getValue() {
			return this.value;
		}
	}

	public static void main(String[] args) {
		LocationStateTag locationStateTag = new LocationStateTag();
		locationStateTag.setLocation(IsValid.NO, LAT.SOUTH, LON.WEST);
	}
	 /**
	 *
	 */
	@Override
	public IDictsEnumTag valueOf(byte pByte) {
		String locaStr = ByteArrayUtil.byteToBit(pByte);
		byte[] locationBits = new byte[8];
		char [] charLocation = locaStr.toCharArray();
		for(int i =0;i<charLocation.length;i++){
			locationBits[i] = Byte.parseByte(String.valueOf(charLocation[i]));
		}
		LocationStateTag location = new LocationStateTag();
		location.setIsValid(IsValid.valOf(locationBits[7]));
		location.setLat(LAT.valOf(locationBits[6]));
		location.setLon(LON.valOf(locationBits[5]));
		//System.out.println(location.getK());
		return location;
	}
}
